package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;

public class ClassNoteController  implements Initializable {

    @FXML
    private Button showSchedule;

    @FXML
    private Button searchRommTable;

    @FXML
    private Button SearchTecherTable;

    @FXML
    private Button ClassNote;

    @FXML
    private Button newNote;

    @FXML
    private Button deleteNote;

    @FXML
    private Button seeNote;
    @FXML
    private ChoiceBox<String> selectCourse;

    @FXML
    private TableView<Note> NoteTable;
    @FXML
    private TableColumn<Note, Integer> noteOrder;

    @FXML
    private TableColumn<Note, String> noteTitle;

    @FXML
    private TableColumn<Note, String> noteContent;

    ObservableList<Note> list= FXCollections.observableArrayList();
    String xuehao;
    String[] courseList;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        NoteTable.setVisible(false);

        //得到学生学号
        try {
            Scanner input=new Scanner(new File("src/res/info.txt"));
            while (input.hasNext())
            {
                xuehao=input.next();
            }
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        selectCourse.getItems().addAll(MysqlConn.getCourseListOnly(xuehao));

        noteOrder.setCellValueFactory(new PropertyValueFactory<>("order"));
        noteTitle.setCellValueFactory(new PropertyValueFactory<>("title"));
        noteContent.setCellValueFactory(new PropertyValueFactory<>("content"));
        NoteTable.setItems(list);

    }

    @FXML
    void showNote(ActionEvent event) {
        String courseSelected=selectCourse.getValue();
        String[][] note=MysqlConn.getContent(xuehao,courseSelected);
        for(int i=0;i<note.length;i++)
        {
            list.add(new Note(note[i][0],note[i][1],note[i][2]));
        }
        NoteTable.setVisible(true);
    }
        /*
=================================================================以下是界面转换====================================================================
     */

    @FXML
    void showSchedulePage(ActionEvent event) {
        SceneManager.SwitchToSchedulePage(event);
    }
    @FXML
    void SwitchToDeletePage(ActionEvent event) {
        SceneManager.SwitchToDeleteNotePage(event);
    }

    @FXML
    void SwitchToNewNotePage(ActionEvent event) {
        SceneManager.SwitchToAddNotePage(event);
    }

    @FXML
    void SwitchToNotePage(ActionEvent event) {
        SceneManager.SwitchToNotePage(event);
    }

    @FXML
    void SwitchToSearchRoomTtblePage(ActionEvent event) {
        SceneManager.SwitchToSearchRoomTtblePage(event);
    }

    @FXML
    void SwitchToSearchTeacherTtblePage(ActionEvent event) {
        SceneManager.SwitchToSearchTeacherTtblePage(event);
    }

}
